<template>
  <div :class="{'hidden':hidden}" class="pagination-container1">
    <el-pagination
      v-if="isShow"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
     <el-pagination
       v-else
       class="pagination"
       @size-change="handleSizeChange"
       @current-change="handleCurrentChange"
       :current-page.sync="currentPage"
       :page-size.sync="pageSize"
       prev-text="<<"
       next-text=">>"
       :layout="layout"
       :total="total">
     </el-pagination>
    <a v-if="!isShow" class="customGoPagination" @click="handleCurrentChange">GO</a>
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  props: {
    isShow: {
      type: Boolean,
      default: true
    },
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50, 60]
      }
    },
    // 移动端页码按钮的数量端默认值5
    pagerCount: {
      type: Number,
      default: document.body.clientWidth < 992 ? 5 : 7
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  mounted() {
    if (!this.isShow) {
      document.getElementsByClassName('el-pagination__jump')[0].childNodes[0].nodeValue = '跳转到'
      document.getElementsByClassName('el-pagination__jump')[0].childNodes[2].nodeValue = ''
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      if (this.currentPage * val > this.total) {
        this.currentPage = 1
      }
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style lang="scss">
.pagination-container1 {
  position: relative;
  background: #fff;
  padding: 10px;
  text-align: end;
  .pagination{
      display: inline-block;
      /*上下翻页*/
      font-size: 13px;

      .btn-prev, .btn-next {
        width: 40px;
        height: 40px;
        border: 1px solid #ededed;
        padding: 0;
        text-align: center;
        line-height: 40px;
        box-sizing: border-box;

        span {
          width: 40px;
          height: 40px;
          text-align: center;
          line-height: 40px;
          box-sizing: border-box;
        }

        &:disabled {
          color: #c9c9c9;
          background-color: #f6f6f6;
          cursor: not-allowed;
        }
      }

      /*数字分页*/

      .el-pager {
        li.number {
          color: #2675d9;
          height: 40px;
          width: 40px;
          margin: 0 5px;
          line-height: 40px;
          text-align: center;
          border: 1px solid #ededed;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          word-break: break-all;
          font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
        }

        li.active {
          background-color: #2675d9;
          color: #fff;
        }

        li.btn-quicknext {
          transform: translateY(6px);
          border: 1px solid #ededed;
        }

        li.btn-quickprev {
          transform: translateY(6px);
          border: 1px solid #ededed;
        }
      }

      /*跳转分页*/

      .el-pagination__jump {
        color: #999999;
        height: 40px;
        line-height: 40px;
        margin-left: 7px !important;

        .el-pagination__editor.el-input {
          width: 61px;
          height: 40px;
          padding: 0;
          margin-left: 7px;

          .el-input__inner {
            width: 61px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            border: 1px solid #ededed;
            border-radius: 0;
            padding: 0;
          }
        }
      }
  }
  .customGoPagination {
    display: inline-block;
    background-color: #2675d9;
    color: #fff;
    width: 40px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    box-sizing: border-box;
    border: 1px solid #ededed;
    position: relative;
    left: 5px;
    top: 12px;
    cursor: pointer;
  }
}

.pagination-container.hidden {
  display: none;
}
</style>
